package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.system.ScenicDailyIncome;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.ScenicDailyIncomeMapper;
import com.ruoyi.system.service.IScenicDailyIncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 景区每日收入Service业务层处理
 *
 * @author zjw
 * @date 2023-09-21
 */
@Service
public class ScenicDailyIncomeServiceImpl implements IScenicDailyIncomeService {
    @Autowired
    private ScenicDailyIncomeMapper scenicDailyIncomeMapper;

    /**
     * 查询景区每日收入
     *
     * @param incomeId 景区每日收入主键
     * @return 景区每日收入
     */
    @Override
    public ScenicDailyIncome selectScenicDailyIncomeByIncomeId(Long incomeId) {
        return scenicDailyIncomeMapper.selectScenicDailyIncomeByIncomeId(incomeId);
    }

    /**
     * 查询景区每日收入列表
     *
     * @param scenicDailyIncome 景区每日收入
     * @return 景区每日收入
     */
    @Override
    public List<ScenicDailyIncome> selectScenicDailyIncomeList(ScenicDailyIncome scenicDailyIncome) {
        return scenicDailyIncomeMapper.selectScenicDailyIncomeList(scenicDailyIncome);
    }

    /**
     * 新增景区每日收入
     *
     * @param scenicDailyIncome 景区每日收入
     * @return 结果
     */
    @Override
    public int insertScenicDailyIncome(ScenicDailyIncome scenicDailyIncome) {
        scenicDailyIncome.setCreateTime(DateUtils.getNowDate());
        return scenicDailyIncomeMapper.insertScenicDailyIncome(scenicDailyIncome);
    }

    /**
     * 修改景区每日收入
     *
     * @param scenicDailyIncome 景区每日收入
     * @return 结果
     */
    @Override
    public int updateScenicDailyIncome(ScenicDailyIncome scenicDailyIncome) {
        scenicDailyIncome.setUpdateTime(DateUtils.getNowDate());
        return scenicDailyIncomeMapper.updateScenicDailyIncome(scenicDailyIncome);
    }

    /**
     * 批量删除景区每日收入
     *
     * @param incomeIds 需要删除的景区每日收入主键
     * @return 结果
     */
    @Override
    public int deleteScenicDailyIncomeByIncomeIds(String[] incomeIds) {
        return scenicDailyIncomeMapper.deleteScenicDailyIncomeByIncomeIds(incomeIds);
    }

    /**
     * 删除景区每日收入信息
     *
     * @param incomeId 景区每日收入主键
     * @return 结果
     */
    @Override
    public int deleteScenicDailyIncomeByIncomeId(String incomeId) {
        return scenicDailyIncomeMapper.deleteScenicDailyIncomeByIncomeId(incomeId);
    }

    @Override
    public ScenicDailyIncome selectScenicDailyIncomeByCreateTime(Date createTime) {
        return scenicDailyIncomeMapper.selectScenicDailyIncomeByCreateTime(createTime);
    }

    @Override
    public ScenicDailyIncome selectScenicDailyIncome(ScenicDailyIncome scenicDailyIncome) {
        return scenicDailyIncomeMapper.selectScenicDailyIncome(scenicDailyIncome);
    }

    @Override
    public double getAccumulatedTotalIncome(Long scenicId) {
        ScenicDailyIncome scenicDailyIncome = new ScenicDailyIncome();
        scenicDailyIncome.setScenicId(scenicId);
        List<ScenicDailyIncome> scenicDailyIncomeList = selectScenicDailyIncomeList(scenicDailyIncome);
        if (Objects.nonNull(scenicDailyIncomeList) && scenicDailyIncomeList.size() > 0) {
            BigDecimal accumulatedTotalIncome = scenicDailyIncomeList.stream().map(item -> item.getOrderFeeIncome()).reduce((x, y) -> x.add(y)).get();
            return accumulatedTotalIncome.setScale(2, BigDecimal.ROUND_UP).doubleValue();
        } else {
            return 0;
        }
    }

}
